from django.db import models

from bases.models import User
from questions_management.models import ProjectTopic


class TopicSelection(models.Model):
    """选题申请记录"""
    STATUS_CHOICES = (
        ('applied', '已申请'),
        ('approved', '已通过'),
        ('rejected', '未通过'),
        ('dropped', '已退题'),
    )
    student = models.ForeignKey(User, on_delete=models.CASCADE, related_name='selected_topics', verbose_name="学生")
    topic = models.ForeignKey(ProjectTopic, on_delete=models.CASCADE, related_name='selections', verbose_name="题目")
    status = models.CharField(max_length=20, choices=STATUS_CHOICES, default='applied', verbose_name="状态")
    apply_time = models.DateTimeField(auto_now_add=True, verbose_name="申请时间")
    approve_time = models.DateTimeField(null=True, blank=True, verbose_name="审核时间")
    comment = models.TextField(null=True, blank=True, verbose_name="审核意见")

    class Meta:
        verbose_name = "选题申请"
        verbose_name_plural = "选题流程管理"
        unique_together = ('student', 'topic')  # 防止重复申请

    def __str__(self):
        return f"{self.student.username} - {self.topic.title}"
